package com.zb.config;

import com.bstek.ureport.UReportPropertyPlaceholderConfigurer;
import com.bstek.ureport.console.UReportServlet;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.annotation.Resource;
import javax.sql.DataSource;

@Configuration
@SuppressWarnings({"rawtypes", "unchecked"})
@ImportResource("classpath:ureport-console-context.xml")
public class UreportConfig {

    /**
     * 配置访问路径Bean
     */
    @Bean
    public ServletRegistrationBean webServletRegistrationBean() {
        ServletRegistrationBean bean = new ServletRegistrationBean();
        bean.setServlet(new UReportServlet());
        bean.addUrlMappings("/ureport/*");
        bean.setName("ureportServlet");
        return bean;
    }

    /**
     * 配置内置数据源
     */
    @Primary
    @Bean(name = "defaultDs")
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource createDefaultDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Autowired
    @Bean(name = "jdbcTemplate")
    public JdbcTemplate createJdbcTemplate(@Qualifier("defaultDs") DataSource defaultDataSource) {
        return new JdbcTemplate(defaultDataSource);
    }

}
